package com.chen.librarysystem.context;

import com.chen.librarysystem.service.BookService;
import com.chen.librarysystem.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;

import java.util.Scanner;

/**
 * 使用单例模式创建全局上下文对象
 *
 * @author Chen
 */
@Setter
@Getter
public class AppContext {
    private static volatile AppContext instance;
    private Scanner scanner;
    private ObjectMapper mapper;
    private String userName;
    private String password;

    private AppContext() {
    }

    public Scanner getScanner() {
        if (scanner == null) {
            scanner = new Scanner(System.in);
        }
        return scanner;
    }

    public static AppContext getInstance() {
        if (instance == null) {
            synchronized (AppContext.class) {
                if (instance == null) {
                    instance = new AppContext();
                }
            }
        }
        return instance;
    }
}
